package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.swagger.exception.GgktException;
import com.atguigu.ggkt.swagger.result.Result;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.atguigu.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-09-21
 */
@Api(tags = "讲师接口管理")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    //查询所有讲师
    @ApiOperation("查询所有讲师")
    @GetMapping("/findAll")
    public Result list(){
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("查询所有讲师成功！");
    }
    //逻辑删除所有讲师
    @ApiOperation("根据id逻辑删除讲师")
    @DeleteMapping("/remove/{id}")
    public Result remove(@ApiParam("ID") @PathVariable("id") Integer id){
        boolean isDel = teacherService.removeById(id);
        if (isDel){
            return Result.ok(null);
        }
        else {
            return Result.fail(null);
        }
    }
    //分页
    @ApiOperation("分页条件查询")
    @PostMapping("/findQueryPage/{current}/{limit}")
    public Result page(@PathVariable("current") long current,
                       @PathVariable("limit") long limit,
                       @RequestBody TeacherQueryVo teacherQueryVo){

        //构造分页条件
        Page<Teacher> pageInfo=new Page<>(current,limit);
        //分页结果
        IPage<Teacher> teacherPage;
        //条件为空，查询所有
        if(teacherQueryVo==null){
            teacherPage = teacherService.page(pageInfo, null);
        }else {
            //构造查询条件
            LambdaQueryWrapper<Teacher> queryWrapper=new LambdaQueryWrapper<>();
            //查询条件
            queryWrapper
                    //姓名匹配
                    .like(teacherQueryVo.getName()!=null,Teacher::getName,teacherQueryVo.getName())
                    //头衔匹配
                    .eq(teacherQueryVo.getLevel()!=null,Teacher::getLevel,teacherQueryVo.getLevel())
                    //入驻开始时间-入驻结束时间
                    .ge(teacherQueryVo.getJoinDateBegin()!=null,Teacher::getJoinDate,teacherQueryVo.getJoinDateBegin())
                    .le(teacherQueryVo.getJoinDateEnd()!=null,Teacher::getJoinDate,teacherQueryVo.getJoinDateEnd());
            //进行分页
            teacherPage = teacherService.page(pageInfo, queryWrapper);
        }
        return Result.ok(teacherPage);
    }
    @ApiOperation(value = "新增")
    @PostMapping("/saveTeacher")
    public Result save(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return Result.ok(null);
    }
    @ApiOperation(value = "获取")
    @GetMapping("/getTeacher/{id}")
    public Result get(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }
    @ApiOperation(value = "修改")
    @PostMapping("/updateTeacher")
    public Result updateById(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return Result.ok(null);
    }
    @ApiOperation(value = "根据id列表逻辑删除")
    @DeleteMapping("/removeBatch")
    public Result batchRemove(@RequestBody List<Long> idList) {
        teacherService.removeByIds(idList);
        return Result.ok(null);
    }
}

